<?php
/**
 * Created By virus_cms
 * Author: Virus
 * Date: 2020/2/29
 * Time: 15:51
 */

namespace app\admin\service;


use app\common\model\mysql\ConfigGroup;

class ConfigGroupService
{
    protected $configGroupModel = null;

    public function __construct()
    {
        $this->configGroupModel = new ConfigGroup();
    }

    /**
     * 添加配置组
     * @param $data
     * @return bool
     * @throws \think\Exception
     */
    public function addConfigGroup($data)
    {
        $res = $this->configGroupModel->add($data);
        if (false === $res) {
            throw new \think\Exception("配置组添加失败");
        }

        return true;
    }

    /**
     * @desc 分页查询所有配置组信息
     * @return \think\Paginator
     * @throws \think\Exception
     * @throws \think\db\exception\DbException
     */
    public function listOfConfigGroup()
    {
        $res = $this->configGroupModel->listOfConfigGroup();
        if (empty($res)) {
            throw new \think\Exception("获取数据失败");
        }

        return $res;
    }

    /**
     * 根据id获取配置组信息
     * @param $id
     * @return array|bool|\think\Model|null
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getConfigGroupById($id)
    {
        $res = $this->configGroupModel->getConfigGroupById($id);
        if (empty($res)) {
            throw new \think\Exception("获取数据失败");
        }
        $res = $res->toArray();

        return $res;
    }

    /**
     * @param $id
     * @param $data
     * @return bool
     * @throws \think\Exception
     */
    public function updateById($id, $data)
    {
        $res = $this->configGroupModel->updateById($id, $data);
        if (false === $res) {
            throw new \think\Exception("配置组信息更新失败");
        }

        return true;
    }

    /**
     * @param $id
     * @return bool
     * @throws \think\Exception
     */
    public function deleteById($id)
    {
        $res = $this->configGroupModel->deleteById($id);
        if (false === $res) {
            throw new \think\Exception("配置组删除失败");
        }

        return true;
    }

    /**
     * 获取所有配置组信息,只取id，group_title
     * @return array|\think\Collection
     * @throws \think\Exception
     */
    public function getAllGroups()
    {
        $res = $this->configGroupModel->getAllGroups();
        if (empty($res)) {
            throw new \think\Exception("获取配置组信息失败");
        }
        $res = $res->toArray();

        return $res;
    }
}